package com.ydzh.kfqgwvisitor.base;

import com.alibaba.fastjson.JSON;

import com.ydzh.kfqgwvisitor.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "返回信息对象", description = "返回信息对象")
public class Result<T> implements Serializable {

    /**
     * 状态码 0 表示成功
     */
    @ApiModelProperty(value = "响应业务状态码")
    private Integer code;
    /**
     * 数据
     */
    @ApiModelProperty(value = "返回值")
    private T data;
    /**
     * 描述
     */
    @ApiModelProperty(value = "响应信息")
    private String msg;

    public Result(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMessage();
        this.data = data;
    }

    public Result(Integer code, T data, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 正确的返回值
     *
     * @param data
     * @return
     */
    public static <T> Result<T> ok(T data) {
        return new Result(data);
    }

    /**
     * @description: 直接返回
     * @param: []
     * @return: com.parenting.plaza.vo.ResultVO
     * @author: zcj
     * @date: 2021/4/30
     */
    public static Result success() {
        return new Result();
    }

    /**
     * 直接返回成功
     */
    public Result() {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMessage();
    }

    /**
     * 成功，不传入数据
     *
     * @return
     */
    public static Result buildSuccess() {

        return new Result(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage());
    }

    /**
     * 成功，传入数据
     *
     * @param data
     * @return
     */
    public static Result buildSuccess(Object data) {
        return new Result(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage());
    }

    /**
     * 失败，传入描述信息
     *
     * @param msg
     * @return
     */
    public static Result buildError(String msg) {
        return new Result(-1, null, msg);
    }


    /**
     * 自定义状态码和错误信息
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result buildCodeAndMsg(int code, String msg) {
        return new Result(code, null, msg);
    }

    /**
     * 传入枚举，返回信息
     *
     * @param codeEnum
     * @return
     */
    public static Result buildResult(ResultEnum codeEnum) {
        return Result.buildCodeAndMsg(codeEnum.getCode(), codeEnum.getMessage());
    }

    public String toJsonString() {
        return JSON.toJSONString(this);
    }
}
